package com.ts.api.interceptor;

import com.gitee.apanlh.spring.BeanContextUtils;
import com.ts.api.module.visitor.service.ChannelVisitorService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 渠道访问拦截器
 */
@Component
public class ChannelVisitorHandler implements HandlerInterceptor {

    private ChannelVisitorService channelVisitorService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        initBean();
        channelVisitorService.insertVisitor();
        return true;
    }

    private void initBean() {
        if (channelVisitorService == null) {
            channelVisitorService = BeanContextUtils.getBean(ChannelVisitorService.class);
        }
    }
}
